/**
 * 自定义函数
 * */


var apiDomain = '/api/h5fuwu';
var apis = {
    toh5code: { //更新H5信息、扫码记录
        method: 'post',
        url: apiDomain + '/toh5code'
    },
    fuwuList: { //服务列表
        method: 'post',
        url: apiDomain + '/list'
    },
    dayList: { //日常列表
        method: 'post',
        url: apiDomain + '/day/list'
    },
    imgList: { //图片列表
        method: 'post',
        url: apiDomain + '/img/list'
    },
}; //后端接口地址
var pageSize = 10;


//底部菜单
var ms = [
    {id: 1, title: '服务项目', link: 'index.html' },
    {id: 2, title: '工作日常', link: 'work.html' },
    {id: 3, title: '加我优惠', link: 'my.html' },
];
function menuList(){
    var h = '', i;
    for (i = 0; i < ms.length; i ++) {
        if (ms[i].id == menu_id) {
            h += '<div class="click">' + ms[i].title + '</div>';
        } else {
            h += '<div onclick="toMenu(' + i + ');">' + ms[i].title + '</div>';
        }
    }
    $('#menu').html(h);
}
function toMenu(v){
    var link = '';
    for (var i = 0; i < ms.length; i ++) {
        if (i == v) {
            link = ms[i].link;
        }
    }
    if (link == '') {
        msgPop({msg: '没有URL'}); return
    }
    window.location.href = link;
}



//显示前几个字符
function strPrefix(str, limit){
    if (str.length > limit) {
        return str.substring(0, limit) + '...';
    } else {
        return str;
    }
}


//显示后几个字符
function strSuffix(str, limit){
    if (str.length > limit) {
        return '...' + str.slice(- limit);
    } else {
        return str;
    }
}


//自定义弹窗
function msgPop(e){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var msg = e.msg;
    var cwb = cw * 0.5; cwb = cwb>500 ? 500 : cwb;
    var h = '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch - 35) + 'px; background: #000; opacity: 0.2; position: fixed; top: 35px; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.5) + 'px; text-align: center; background: #000; color: #FFF; border-radius: 5px; position: fixed; bottom: 40%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">'+msg+'</div>';
    h += '</div>';
    h += '<script>setTimeout(function(){$(\'#msgPop\').html(\'\');},2000)</script>';
    $('#msgPop').html(h);
}


//抓图
function uploadImage() {
    var img = $("#viewimg"), dataURL, fileObj = $('#uploadFile')[0],
        windowURL = window.URL || window.webkitURL, reader = new FileReader();
    dataURL = windowURL.createObjectURL(fileObj.files[0]); //要显示图片的路径
    img.attr("src", dataURL); //显示图片
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (e){
        showImg(e.target.result);
    }
}


//获取地址栏中参数
function getParamByUrl(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r==null) return '';
    return  unescape(r[2]);
}


//生成随机数
function randInt(min, max) {
    // 加1是因为我们想要包含max值在内
    return Math.floor(Math.random() * (max - min + 1)) + min;
}


//获取登录信息
function getUserInfo(){
    var cache = window.localStorage.getItem(userLogin);
    if (!cache) { return ''; }
    return JSON.parse(cache);
}


//设置登录信息
function setUserInfo(e){
    window.localStorage.setItem(userLogin, JSON.stringify(e.val));
}


//获取缓存
function getCache(key){
    var cache = window.localStorage.getItem(key);
    if (!cache) { return ''; }
    return JSON.parse(cache);
}


//设置缓存
function setCache(e){
    window.localStorage.setItem(e.key, JSON.stringify(e.val));
}